package com.mrd.util;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


public class ZipUtil {
    public static String add(List<String> paths) {
        String zipPath = "D:/" + new SimpleDateFormat("yyyyMMddhhMMss").format(new Date()) + ".zip";
        try {
            File zipFile = new File(zipPath);
            Project project = new Project();
            Zip zip = new Zip();
            zip.setProject(project);
            zip.setDestFile(zipFile);
            FileSet fileSet = new FileSet();
            fileSet.setProject(project);
            for (int i = 0; i < paths.size(); i++) {
                File file = new File(paths.get(i).toString());
                if (file.exists()) {
                    fileSet.setFile(file);
                } else {
                    System.out.println(file.getName() + "===>文件不存在");
                }
            }
            zip.setEncoding("GBK");
            zip.addFileset(fileSet);
            zip.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return zipPath;
    }

    public static String add(String paths) {
        String zipPath = "D:/" + new SimpleDateFormat("yyyyMMddhhMMss").format(new Date()) + ".zip";
        try {
            File zipFile = new File(zipPath);
            Project project = new Project();
            Zip zip = new Zip();
            zip.setProject(project);
            zip.setDestFile(zipFile);
            FileSet fileSet = new FileSet();
            fileSet.setProject(project);
            File file = new File(paths);
            if (file.exists()) {
                fileSet.setFile(file);
            } else {
                System.out.println(file.getName() + "===>文件不存在");
            }
            zip.setEncoding("GBK");
            zip.addFileset(fileSet);
            zip.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return zipPath;
    }

    public static void removeFile(String zipPath) {
        File zipFile = new File(zipPath);
        zipFile.delete();
    }

    public static void main(String[] args) {
        List<String> paths = new ArrayList<String>();
        paths.add("D:\\BGRenameTest\\2014年报关底单\\04月份\\02号\\石湾祺丰\\894512583佛山市外经企业有限公司（2）.pdf");
        paths.add("D:\\BGRenameTest\\2014年报关底单\\04月份\\01号\\石湾祺丰\\894512583佛山市外经企业有限公司.pdf");
        add(paths);
    }

    /**
     * 将多个文件压缩成.ZIP文件
     *
     * @param filePaths   需要压缩的文件地址
     * @param zipFilePath 压缩后的文件地址
     * @return 只有返回success是操作完全成功
     */
    public static String toZIP(String[] filePaths, String zipFilePath) {
        String result = "success";
        File[] srcfile = new File[filePaths.length];
        for (int i = 0; i < srcfile.length; i++) {
            srcfile[i] = new File(filePaths[i]);
        }
        File zipfile = new File(zipFilePath);
        byte[] buf = new byte[1024];
        ZipOutputStream out = null;
        try {
            out = new ZipOutputStream(new FileOutputStream(zipfile));
            for (int i = 0; i < srcfile.length; i++) {
                FileInputStream in = null;
                try {
                    in = new FileInputStream(srcfile[i]);
                    out.putNextEntry(new ZipEntry(srcfile[i].getName()));
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    result = getResult(result, e.getMessage());
                } finally {
                    try {
                        out.closeEntry();
                        if (in != null) {
                            in.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                        result = getResult(result, e.getMessage());
                    } finally {
                        in = null;
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            result = getResult(result, e.getMessage());
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                result = getResult(result, e.getMessage());
            } finally {
                out = null;
                if (!"success".equals(result)) {
                    zipfile.delete();
                }
            }
        }
        return result;
    }

    private static String getResult(String oldResult, String newResult) {
        if ("success".equals(oldResult)) {
            return newResult;
        } else {
            return oldResult + System.getProperty("line.separator") + newResult;
        }
    }
}
